package com.score.controller;

import com.score.pojo.ResponseEntry;
import com.score.service.impl.UpDownLoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;

@RestController
@RequestMapping("/updownload")
public class UpDownLoadController {

    @Autowired
    private UpDownLoadService upDownLoadService;

    @Secured({"ROLE_headmaster","ROLE_instructor","ROLE_deputySecretary","ROLE_dean"})
    @GetMapping("/exceldown")
    public ResponseEntry down(HttpServletResponse response,String position,String className,String college){
        try {
            upDownLoadService.excel(response,position,className,college);
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntry().fail("下载失败",e.getMessage());
        }
    }

    @Secured({"ROLE_headmaster","ROLE_instructor","ROLE_deputySecretary","ROLE_dean"})
    @GetMapping("/downUnpass")
    public ResponseEntry downUnpass(HttpServletResponse response,String position,String className,String college){
        try {
            upDownLoadService.downUnpass(response,position,className,college);
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntry().fail("下载失败",e.getMessage());
        }
    }

    @Secured({"ROLE_headmaster","ROLE_instructor","ROLE_deputySecretary","ROLE_dean"})
    @PostMapping("/uploadExcel")
    public ResponseEntry UploadExcel(MultipartFile file){
        try {
            return upDownLoadService.UploadExcel(file);
        } catch (ParseException e) {
            e.printStackTrace();
            return new ResponseEntry().fail("no",null);
        }

    }

}
